package com.liuyc.lib.ui.xrecycleview

import android.util.SparseArray
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager

class CustomWrapAdapter(headerViews: SparseArray<View>, footViews: SparseArray<View>, adapter: RecyclerView.Adapter<RecyclerView.ViewHolder>?) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    val TYPE_REFRESH_HEADER = -5
    val TYPE_HEADER = -4
    val TYPE_NORMAL = 0
    val TYPE_FOOTER = -3
    var mAdapter: RecyclerView.Adapter<RecyclerView.ViewHolder>? = null

    var mHeaderViews: SparseArray<View>? = null

    var mFootViews: SparseArray<View>? = null

    var headerPosition = 1

    init {
        mAdapter = adapter
        mHeaderViews = headerViews
        mFootViews = footViews
    }

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        val manager: RecyclerView.LayoutManager = recyclerView.layoutManager!!
        if (manager is GridLayoutManager) {
            val gridManager: GridLayoutManager = manager as GridLayoutManager
            gridManager.setSpanSizeLookup(object : GridLayoutManager.SpanSizeLookup() {
                override fun getSpanSize(position: Int): Int {
                    return if (isHeader(position) || isFooter(position)) gridManager.getSpanCount() else 1
                }
            })
        }
    }

    override fun onViewAttachedToWindow(holder: RecyclerView.ViewHolder) {
        super.onViewAttachedToWindow(holder)
        val lp: ViewGroup.LayoutParams = holder.itemView.getLayoutParams()
        if (lp != null && lp is StaggeredGridLayoutManager.LayoutParams
                && (isHeader(holder.getLayoutPosition()) || isFooter(holder.getLayoutPosition()))) {
            val p: StaggeredGridLayoutManager.LayoutParams = lp as StaggeredGridLayoutManager.LayoutParams
            p.setFullSpan(true)
        }
    }

    fun isHeader(position: Int): Boolean {
        return position >= 0 && position < mHeaderViews!!.size()
    }

    fun isFooter(position: Int): Boolean {
        return position < getItemCount() && position >= getItemCount() - mFootViews!!.size()
    }

    fun isRefreshHeader(position: Int): Boolean {
        return position == 0
    }

    fun getHeadersCount(): Int {
        return mHeaderViews!!.size()
    }

    fun getFootersCount(): Int {
        return mFootViews!!.size()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (viewType == TYPE_REFRESH_HEADER) {
            return SimpleViewHolder(mHeaderViews!![0])
        } else if (viewType == TYPE_HEADER) {
            return SimpleViewHolder(mHeaderViews!![headerPosition++])
        } else if (viewType == TYPE_FOOTER) {
            return SimpleViewHolder(mFootViews!![0])
        }
        return mAdapter?.onCreateViewHolder(parent, viewType)!!
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (isHeader(position)) {
            return
        }
        val adjPosition = position - getHeadersCount()
        val adapterCount: Int
        if (mAdapter != null) {
            adapterCount = mAdapter!!.getItemCount()
            if (adjPosition < adapterCount) {
                mAdapter!!.onBindViewHolder(holder, adjPosition)
            }
        }
    }

    override fun getItemCount(): Int {
        return if (mAdapter != null) {
            getHeadersCount() + getFootersCount() + mAdapter!!.itemCount
        } else {
            getHeadersCount() + getFootersCount()
        }
    }

    override fun getItemViewType(position: Int): Int {
        if (isRefreshHeader(position)) {
            return TYPE_REFRESH_HEADER
        }
        if (isHeader(position)) {
            return TYPE_HEADER
        }
        if (isFooter(position)) {
            return TYPE_FOOTER
        }
        val adjPosition = position - getHeadersCount()
        val adapterCount: Int
        if (mAdapter != null) {
            adapterCount = mAdapter!!.itemCount
            if (adjPosition < adapterCount) {
                return mAdapter!!.getItemViewType(adjPosition)
            }
        }
        return TYPE_NORMAL
    }

    override fun getItemId(position: Int): Long {
        if (mAdapter != null && position >= getHeadersCount()) {
            val adjPosition = position - getHeadersCount()
            val adapterCount: Int = mAdapter!!.itemCount
            if (adjPosition < adapterCount) {
                return mAdapter!!.getItemId(adjPosition)
            }
        }
        return -1
    }


    override fun unregisterAdapterDataObserver(observer: RecyclerView.AdapterDataObserver) {
        if (mAdapter != null) {
            mAdapter!!.unregisterAdapterDataObserver(observer)
        }
    }

    override fun registerAdapterDataObserver(observer: RecyclerView.AdapterDataObserver) {
        if (mAdapter != null) {
            mAdapter!!.registerAdapterDataObserver(observer)
        }
    }

    class SimpleViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
}